package com.xishu.service.print.kitchen.item1order1;

import com.xishu.entity.hardware.Printer;
import com.xishu.entity.order.Order;
import com.xishu.entity.order.OrderItem;
import com.xishu.service.print.kitchen.KitchenService;
import com.xishu.util.Tools;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class PrintOrderCmd implements Command, Tools {
    private static Logger logger = LoggerFactory.getLogger(PrintOrderCmd.class);
    private Order printerOrder;
    private Printer printer;

    public PrintOrderCmd(Order printerOrder, Printer printer) {
        this.printerOrder = printerOrder;
        this.printer = printer;
    }

    @Override
    public boolean execute(Context context) throws Exception {
        for (OrderItem orderItem : getList(printerOrder.getOrderItemDetailList())) {
            Order oneItemOneOrder = new Order();
            BeanUtils.copyProperties(printerOrder, oneItemOneOrder);

            List<OrderItem> orderItemList = new ArrayList<>();
            orderItemList.add(orderItem);
            oneItemOneOrder.setOrderItemDetailList(orderItemList);

            logger.info("will print order item is {}, itemId {}", orderItem.getId(), orderItem.getItemId());

            //有几份就打印几次
            for (Integer i = 0; i < orderItem.getCount(); i++) {
                //依次打印
                KitchenService.getInstance().newPrintOneOrderWhenSendToKitchen(oneItemOneOrder, printer);
            }
        }

        return false;
    }
}
